package com.webapp.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.inforim.cognos.CognosClient;
import com.inforim.cognos.utils.CognosHelper;
import com.webapp.loader.XmlReportsLoader;
import com.webapp.module.MailSubscriber;
import com.webapp.module.Report;
import com.webapp.service.ReportService;
import com.webapp.util.HandleReport;
import com.webapp.util.ParamUtil;

@Controller
@RequestMapping("/report")
public class ReportController {

	static Logger logger = Logger.getLogger(ReportController.class);
	@Autowired
	ReportService reportService;
	@Autowired
	XmlReportsLoader xmlReportsLoader;
	
	@RequestMapping("/run")
	@ResponseBody
	public String run(HttpServletRequest request,@RequestBody String param) {
		logger.debug("------run---{}");
//		XmlReportsLoader xmlReportsLoader = new XmlReportsLoader();
//		xmlReportsLoader.load();
		CognosClient cognosClient = CognosHelper.getClient();
		cognosClient.setDispatch(xmlReportsLoader.getC8_URL());
		cognosClient.setUsername(xmlReportsLoader.getC8_USERNAME());
		cognosClient.setPassword(xmlReportsLoader.getC8_PASSWORD());
		cognosClient.setNamespace(xmlReportsLoader.getC8_NAMESPACE());
		try {
			cognosClient.logon();
		} catch (LoginException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.err.println("------完成------");
		Map reports = xmlReportsLoader.getReportList();
		String format = null;
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1); // DATE=5 前一天
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		MailSubscriber subscriber = null;
		List attachfiles = new ArrayList();
		String attachfile = null;
		JSONObject jsonObject = JSON.parseObject(param);
		ParamUtil paramUtil = new ParamUtil();
		List<String> paramList = paramUtil.getParamListString(jsonObject);
		String paramString = paramUtil.getParamString(jsonObject);
		Report report = (Report)reports.get("000");
		report.setSaveSheetfolder("/"+System.currentTimeMillis()+"/");
		if (paramList != null) {
			for (String string : paramList) {
				reportService.runReport(cognosClient, report, format, cal, string +"|"+paramString,subscriber, attachfiles, attachfile);//连接、报表、null,时间，参数设置
			}
		}else {
			reportService.runReport(cognosClient, report, format, cal, paramString,subscriber, attachfiles, attachfile);//连接、报表、null,时间，参数设置
		}
		
		String path = ((Report)reports.get("000")).getHKSaveSheetPath();
		HandleReport hReport = new HandleReport();
		hReport.getHandleReport(path);
		return param;
	}
	
	
	
	
}
